using Assets.Scripts.GDK;
using SimpleJson;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class DailyPanel : gdk_base_panel
	{
		public RectTransform scrollContent;

		public RectTransform rowItemBox;

		public RectTransform sevenItemBox;

		public int curSignDay = 1;

		private bool bSigned;

		public Button signButton;

		private List<RectTransform> daysItem;

		public override void OnLoad()
		{
			initData();
		}

		public void OnSignClick()
		{
			if (false)
			{
				gdk_game_manager.PlaySound(AudioID.click);
				GameManager.OnSignDay(curSignDay);
				GameManager.sendCustomEvent(CustomEventID.DAYLY_SIGN, new List<string>
		{
			curSignDay.ToString()
		});
				if (curSignDay == 7)
				{
					DayModel[] days7Array = GameData.Days7Array;
					GameManager.showRewardsAlert(days7Array);
					sevenItemBox.GetComponent<DailySevenItem>().OnSign();
				}
				else
				{
					DayModel[] daysArray = GameData.DaysArray;
					for (int i = 0; i < daysArray.Length; i++)
					{
						if (daysArray[i].Day == curSignDay)
						{
							GDK.ModuleManger.Module<GameSceneModule>().OnGainTools(daysArray[i].rewardType, daysArray[i].rewardCount);
							GameManager.showAddCoinAlert(daysArray[i].rewardCount, daysArray[i].rewardType);
							break;
						}
					}
					daysItem[curSignDay - 1].GetComponent<DailyItem>().OnSign();
				}
				enableBtn();
			}
			else
			{
				gdk_game_manager.PlaySound(AudioID.click);
						GameManager.OnSignDay(curSignDay);
						GameManager.sendCustomEvent(CustomEventID.DAYLY_SIGN, new List<string>
		{
			curSignDay.ToString()
		});
						if (curSignDay == 7)
						{
							DayModel[] days7Array = GameData.Days7Array;
							GameManager.showRewardsAlert(days7Array);
							sevenItemBox.GetComponent<DailySevenItem>().OnSign();
						}
						else
						{
							DayModel[] daysArray = GameData.DaysArray;
							for (int i = 0; i < daysArray.Length; i++)
							{
								if (daysArray[i].Day == curSignDay)
								{
									GDK.ModuleManger.Module<GameSceneModule>().OnGainTools(daysArray[i].rewardType, daysArray[i].rewardCount);
									GameManager.showAddCoinAlert(daysArray[i].rewardCount, daysArray[i].rewardType);
									break;
								}
							}
							daysItem[curSignDay - 1].GetComponent<DailyItem>().OnSign();
						}
						enableBtn();
				
			}
		}

		public void enableBtn()
		{
			signButton.interactable = false;
		}

		public void initData()
		{
			rowItemBox.gameObject.SetActive(value: true);
			gdk_utils.removeAllChildren(scrollContent);
			daysItem = new List<RectTransform>();
			checkSignDay();
			if (bSigned)
			{
				enableBtn();
			}
			DayModel[] daysArray = GameData.DaysArray;
			DayModel[] days7Array = GameData.Days7Array;
			foreach (DayModel dayModel in daysArray)
			{
				RectTransform rectTransform = UnityEngine.Object.Instantiate(rowItemBox);
				rectTransform.gameObject.SetActive(value: true);
				rectTransform.SetParent(scrollContent, worldPositionStays: false);
				bool isSign = IsSign(dayModel.Day);
				rectTransform.GetComponent<DailyItem>().OnData(dayModel, isSign);
				daysItem.Add(rectTransform);
			}
			bool isSign2 = IsSign(7);
			sevenItemBox.GetComponent<DailySevenItem>().OnData(days7Array, isSign2);
			rowItemBox.gameObject.SetActive(value: false);
		}

		public bool IsSign(int day)
		{
			List<int> signDays = GameManager.GetSignDays();
			if (signDays == null)
			{
				return false;
			}
			for (int i = 0; i < signDays.Count; i++)
			{
				if (i + 1 == day)
				{
					return true;
				}
			}
			return false;
		}

		public void checkSignDay()
		{
			List<int> signDays = GameManager.GetSignDays();
			if (signDays == null || signDays.Count == 0)
			{
				curSignDay = 1;
			}
			else
			{
				curSignDay = signDays.Count;
				DateTime today = DateTime.Today;
				if (today.DayOfWeek.ToString() == "Sunday")
				{
					if (signDays[0] == today.DayOfYear)
					{
					}
				}
				else
				{
					for (int i = 0; i < signDays.Count; i++)
					{
						if (signDays[i] == today.DayOfYear)
						{
							bSigned = true;
						}
					}
					if (!bSigned)
					{
						curSignDay++;
					}
				}
			}
			if (curSignDay == 8)
			{
				List<int> json = new List<int>();
				GameData.dailyRecod = SimpleJson.SimpleJson.SerializeObject(json);
				curSignDay = 1;
			}
		}

		public void OnCloseClick()
		{
			gdk_game_manager.PlaySound(AudioID.click);
			close();
		}
	}
}

